package com.pro01.backend.mapper;

import com.pro01.backend.pojo.Promotion;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface PromotionMapper {
    // 添加促销记录
    @Insert("INSERT INTO promotions (torrent_id, promotion_type, start_time, end_time, created_at) " +
            "VALUES (#{torrentId}, #{promotionType}, #{startTime}, #{endTime}, #{createdAt})")
    int insertPromotion(Promotion promotion);

    // 查询促销记录（带种子名称）
    @Select("""
        SELECT 
            p.id,
            p.torrent_id,
            t.name AS torrent_name,
            p.promotion_type,
            p.start_time,
            p.end_time,
            p.created_at
        FROM promotions p
        LEFT JOIN torrents t ON p.torrent_id = t.id
        ORDER BY p.created_at DESC
        LIMIT #{offset}, #{limit}
        """)
    List<Map<String, Object>> getPromotionHistory(@Param("offset") int offset, @Param("limit") int limit);

    // 查询促销总数（分页需要）
    @Select("SELECT COUNT(*) FROM promotions")
    int countPromotionHistory();

    // 根据种子id查询促销信息
    @Select("SELECT * FROM promotions WHERE torrent_id = #{torrentId} AND NOW() < end_time")
    Promotion getActivePromotionByTorrentId(@Param("torrentId") Long torrentId);

}
